package com.njtoyo.taxi.admin.service.business.impl;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njtoyo.taxi.admin.mapper.master.enums.RealtimeCallCategory;
import com.njtoyo.taxi.admin.mapper.master.mapper.SimpleCallMapper;
import com.njtoyo.taxi.admin.rest.presenter.business.ride.call.RealTimeSinglePagePresenter;
import com.njtoyo.taxi.admin.rest.presenter.platfrom.AdminUserIdentityPresenter;
import com.njtoyo.taxi.admin.rest.wrapper.business.ride.CallWrapper;
import com.njtoyo.taxi.admin.rest.wrapper.business.ride.CallWrapperDto;
import com.njtoyo.taxi.admin.service.business.SimpleCallService;
import com.njtoyo.taxi.entity.backend.AdminUser;
import com.taxi.entity.common.RestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

@Component
public class SimpleCallServiceImpl implements SimpleCallService {

    @Autowired
    private SimpleCallMapper simpleCallMapper;
    @Override
    public RestResult getPageList(AdminUser adminUser, CallWrapper wrapper) {
        if (StrUtil.isNotBlank(wrapper.getStartDate()) && StrUtil.isNotBlank(wrapper.getEndDate())) {
            wrapper.setStartDate(wrapper.getStartDate() + " 00:00:00");
            wrapper.setEndDate(wrapper.getEndDate() + " 23:59:59");
        }
        AdminUserIdentityPresenter identity = (AdminUserIdentityPresenter) adminUser.getIdentity();
        //表中没有公司只能用行政区
        wrapper.setCantonIds(identity.getCantonIds());
        Page<RealTimeSinglePagePresenter> realTimeSingleList
                = simpleCallMapper.selectCallPage(wrapper);
        return RestResult.success(realTimeSingleList);
    }

    @Override
    public void simpleCallListExport(AdminUser adminUser, CallWrapperDto wrapper, HttpServletResponse response) throws IOException {
        if (StrUtil.isNotBlank(wrapper.getStartDate()) && StrUtil.isNotBlank(wrapper.getEndDate())) {
            wrapper.setStartDate(wrapper.getStartDate() + " 00:00:00");
            wrapper.setEndDate(wrapper.getEndDate() + " 23:59:59");
        }else
        {
            //默认查询当天
            wrapper.setStartDate(DateUtil.format(DateUtil.beginOfDay(new Date()), "yyyy-MM-dd HH:mm:ss"));
            wrapper.setEndDate(DateUtil.format(DateUtil.endOfDay(new Date()), "yyyy-MM-dd HH:mm:ss"));
        }
        AdminUserIdentityPresenter identity = (AdminUserIdentityPresenter) adminUser.getIdentity();
        //表中没有公司只能用行政区
        wrapper.setCantonIds(identity.getCantonIds());
        List<RealTimeSinglePagePresenter> realTimeSinglePagePresenters = simpleCallMapper.exportCallList(wrapper);
        String fileName = URLEncoder.encode("一键呼叫", "UTF-8").replaceAll("\\+", "%20");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), RealTimeSinglePagePresenter.class)
                .sheet()
                .doWrite(realTimeSinglePagePresenters);
    }


}
